/**

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

    new Dependent($('shipToStateProvinceGeoId'),$('shipToCountryGeoId'));
    For documentation on setting up options visit 
    http://plugins.jquery.com/project/DependentSelectbySilVeR

   Requires: 
   1) Prototype >= 1.6.1 
   2) event.simulate.js from http://github.com/kangax/protolicious to trigger native change event.
   
    To get reliable behavior, use setValue method on form element for setting value e.g $('billToStateProvinceGeoId').setValue($F('shipToStateProvinceGeoId'));


 */
var Dependent = Class.create({
    initialize: function(childSelectBox, parentSelectBox) {
      this.childSelectBox = childSelectBox;
      this.parentSelectBox = parentSelectBox;
      
      // Having use cross cutting abilities in Dojo, I missed that feature in Prototype until I found way to do it. Vary Powerful, Like it  
      // Wrapping setValue to work around unavailability of events when control is disabled
      this.parentSelectBox.setValue= this.parentSelectBox.setValue.wrap(this.setValueWrapper.bind(this));
      // add event handler to parent select box value change event
      // seems redundant but its not. When setValue is called change event is not fired, and When change event is fired, setValue is not called, they are mutually exclusive
      this.parentSelectBox.observe('change', this.parentValueChangeListener.bind(this));
      
      this.childSelectBox.setValue= this.childSelectBox.setValue.wrap(function (proceed,value){
          proceed(value);
          this.simulate('change');
          return true;
      });
      
      // Keep data for future use.
      this.childSelectOptions = this.childSelectBox.childElements().clone();
      this.setChildOptions();

    },
    
    // event handler for change in value of parent. change event is not fired for disabled control. May not use this for while
    parentValueChangeListener: function(event) {
        var element = Event.element(event);
        this.setChildOptions();
    },
    
    setValueWrapper: function (proceed,value){
        proceed(value);
        this.setChildOptions();
        return true;
    },
    setChildOptions: function(){
        var parentTitle = this.parentSelectBox.options[this.parentSelectBox.selectedIndex].title;
        this.childSelectBox.update(); //Removed old options.
        for (var index = 0; index < this.childSelectOptions.length; ++index) {  // Not using each on array because for some reason reference to this is lost.
            var item = this.childSelectOptions[index];
            if($(item).hasClassName(parentTitle)){
                this.childSelectBox.appendChild(item);
            }
        }

    }
    
});
